/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Country;

import State.StateBean;
import java.sql.*;
import java.util.Vector;

/**
 *
 * @author mishra
 */
public class Query {

    static Connection con = null;

    public static int add(CountryBean cb) throws SQLException {
        int i = 0, k = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into country values(countryid.nextval,initcap(?),sysdate)");
            pre.setString(1, cb.getCountryName());
            i = pre.executeUpdate();
            if (i != 0) {
                k = getMaxId();
            }

        } catch (Exception e) {
            System.out.println(e);
        }finally{
            con.close();
        }
        return i;
    }

    public static int getMaxId() throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select max(country_id) from country");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                i = rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println(e);
        }finally{con.close();}
        return i;
    }

    public static CountryBean viewSpecific(String cname, int val) throws SQLException {

        CountryBean cb = new CountryBean();
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from country where " + cname + "=?");
            pre.setInt(1, val);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                cb.setCountryId(rs.getInt(1));
                cb.setCountryName(rs.getString(2));
                cb.setEntryDate(rs.getDate(3));
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return cb;
    }

    public static Vector viewAll() throws SQLException {
        Vector<Object> list = new Vector<Object>();

        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from country order by country_id");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                CountryBean cb = new CountryBean();
                cb.setCountryId(rs.getInt(1));
                cb.setCountryName(rs.getString(2));
                cb.setEntryDate(rs.getDate(3));
                list.add(cb);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return list;
    }

    public static int edit(CountryBean cb) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update country set country_name=?,entrydate=sysdate where country_id=?");
            pre.setString(1, cb.getCountryName());
            pre.setInt(2, cb.getCountryId());
            i = pre.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static int delete(int id) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("delete from country where country_id=?");
            pre.setInt(1, id);
            i = pre.executeUpdate();
        } catch (Exception e) {
            return i;
        } finally {
            con.close();
        }
        return i;
    }
}
